def combine(n, k):
    res = []
    backtracking(n, k, res, 1, [])
    return res


def backtracking(n, k, res, index, subset):
    if len(subset) == k:
        res.append(subset.copy())
        return
    for i in range(index, n + 1):
        subset.append(i)
        backtracking(n, k, res, i + 1, subset)
        subset.pop()


print(combine(4, 2))
# [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
